package com.wu.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.wu.server.code.Code;
import com.wu.server.entity.Goods;
import com.wu.server.service.GoodsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
public class GoodsController {
    @Autowired
    Code code;
    @Autowired
    GoodsServer goodsServer;
    @GetMapping("goods")
    @ResponseBody
    public Object getGoodsList(
            @RequestParam(value = "id",defaultValue = "") String id,
            @RequestParam(value = "goodsName",defaultValue = "") String goodsName,
            @RequestParam(value = "price",defaultValue = "0,99999999") String price,
            @RequestParam(value = "isSelf",defaultValue = "false") Boolean isSelf,
            @RequestParam(value = "shopName",defaultValue = "") String shopName,
            @RequestParam(value = "tagName", defaultValue = "") String tagName,
            @RequestParam(value = "sort", defaultValue = "id") String sort,
            @RequestParam(value = "order", defaultValue = "desc") String order,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "100") int pageSize
            ) {
        return goodsServer.queryGoods(id,goodsName,shopName,price,isSelf,tagName,sort,order,pageNum,pageSize);
    }
    @PostMapping("goods")
    @ResponseBody
    public Object addGoods(@RequestBody GoodsForm form){
        Goods goods = new Goods();
        goods.title = form.goodsName;
        goods.prince = form.price;
        goods.shop_name = form.shopName;
        goods.shop_src = form.shopSrc;
        goods.sales = form.sales;
        goods.goods_src = form.goodsSrc;
        goods.goods_img = form.goodsImg;
        goods.tag_name = form.tagName;
        if(form.goodsName == null) return code.getMeta(Code.PARAM_ERROR,null);
        return goodsServer.insertGoods(goods);
    }
    @PutMapping("goods")
    @ResponseBody
    public Object updateGoods(@RequestBody GoodsForm form){
        Goods goods = new Goods();
        goods.id = form.id;
        goods.title = form.goodsName;
        goods.prince = form.price;
        goods.shop_name = form.shopName;
        goods.shop_src = form.shopSrc;
        goods.sales = form.sales;
        goods.goods_src = form.goodsSrc;
        goods.goods_img = form.goodsImg;
        goods.tag_name = form.tagName;
        if(form.goodsName == null || form.id == 0) return code.getMeta(Code.PARAM_ERROR,null);
        return goodsServer.updateGoods(goods);
    }
    @DeleteMapping("goods")
    @ResponseBody
    public Object deleteGoods(@RequestParam(value = "id") int id){
        if (id == 0) return code.getMeta(Code.PARAM_ERROR,null);
        return goodsServer.deleteGoods(id);
    }

    private static class GoodsForm{
        public int id;
        public String goodsName;
        public double price;
        public String goodsImg;
        public String goodsSrc;
        public double sales;
        public String tagName;
        public String shopName;
        public String shopImg;
        public String shopSrc;
    }
}
